package com.sgcc.statistics.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.statistics.domain.IndicatorExecutionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.statistics.domain.OIndexBreakdown;
import com.sgcc.statistics.service.IOIndexBreakdownService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * OIndexBreakdownController
 * 
 * @author sgcc
 * @date 2021-04-14
 */
@RestController
@RequestMapping("/OIndexBreakdown")
public class OIndexBreakdownController extends BaseController
{
    @Autowired
    private IOIndexBreakdownService oIndexBreakdownService;

    /**
     * 查询OIndexBreakdown列表
     */
    //@PreAuthorize(hasPermi = "statistics:OIndexBreakdown:list")
    @GetMapping("/list")
    public TableDataInfo list(OIndexBreakdown oIndexBreakdown) throws Exception {
        startPage();
        List<OIndexBreakdown> list = oIndexBreakdownService.selectOIndexBreakdownList(oIndexBreakdown);
        return getDataTable(list);
    }

    /**
     * 导出OIndexBreakdown列表
     */
    //@PreAuthorize(hasPermi = "statistics:OIndexBreakdown:export")
    @Log(title = "OIndexBreakdown", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OIndexBreakdown oIndexBreakdown) throws Exception
    {
        List<OIndexBreakdown> list = oIndexBreakdownService.selectOIndexBreakdownList(oIndexBreakdown);
        ExcelUtil<OIndexBreakdown> util = new ExcelUtil<OIndexBreakdown>(OIndexBreakdown.class);
        util.exportExcel(response, list, "OIndexBreakdown");
    }

    /**
     * 获取OIndexBreakdown详细信息
     */
    //@PreAuthorize(hasPermi = "statistics:OIndexBreakdown:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(oIndexBreakdownService.selectOIndexBreakdownById(id));
    }

    /**
     * 新增OIndexBreakdown
     */
    //@PreAuthorize(hasPermi = "statistics:OIndexBreakdown:add")
    @Log(title = "OIndexBreakdown", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Map map) throws Exception {
        if (map.size() == 0){
            return AjaxResult.error(201,"缺少参数！");
        }
        List<OIndexBreakdown> OIndexBreakdownList = (List<OIndexBreakdown>)map.get("OIndexBreakdownList");
        return toAjax(oIndexBreakdownService.insertOIndexBreakdown(OIndexBreakdownList));
    }

    /**
     * 修改OIndexBreakdown
     */
    //@PreAuthorize(hasPermi = "statistics:OIndexBreakdown:edit")
    @Log(title = "OIndexBreakdown", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(Map map) throws Exception {
        if (map.size() < 0){
            return AjaxResult.error(201,"缺少参数！");
        }
        List<OIndexBreakdown> OIndexBreakdownList = (List<OIndexBreakdown>)map.get("OIndexBreakdownList");
        return toAjax(oIndexBreakdownService.updateOIndexBreakdown(OIndexBreakdownList));
    }

    /**
     * 删除OIndexBreakdown
     */
    //@PreAuthorize(hasPermi = "statistics:OIndexBreakdown:remove")
    @Log(title = "OIndexBreakdown", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(oIndexBreakdownService.deleteOIndexBreakdownByIds(ids));
    }


    /**
     *  省市指标分解
     * */
    @RequestMapping("/provinceIndexBrekdown")
    public AjaxResult provinceIndexBrekdown(@RequestBody OIndexBreakdown oIndexBreakdown) throws Exception {
        if (null == oIndexBreakdown) {
            return AjaxResult.error("参数为空！");
        }
        List<OIndexBreakdown> oIndexBreakdownList = oIndexBreakdownService.provinceIndexBrekdown(oIndexBreakdown);
        return AjaxResult.success(oIndexBreakdownList);
    }

    /*
    *  查询省级分解后的各地市的数据
    * */
    @RequestMapping("/getShengBreakdownCityData")
    public AjaxResult getShengBreakdownCityData(String year){
        if (null == year){
            return AjaxResult.error("参数为空！");
        }
        List<OIndexBreakdown> oIndexBreakdownList = oIndexBreakdownService.getShengBreakdownCityData(year);
        return AjaxResult.success(oIndexBreakdownList);
    }

     /*
    * 查询市的分解任务
    * */
     @RequestMapping("/getOIndexBreakByUnitAndYear")
     public AjaxResult getOIndexBreakByUnitAndYear(String year, String unit){
         if (null == year || null == unit){
             return AjaxResult.error("参数为空！");
         }
         OIndexBreakdown oIndexBreakdown = oIndexBreakdownService.getIndexBrekdownByUnit(year,unit);
         return AjaxResult.success(oIndexBreakdown);
     }

    /**
     * 地市指标分解
     */
    @RequestMapping("/cityIndexBrekdown")
    public AjaxResult cityIndexBrekdown(@RequestBody OIndexBreakdown oIndexBreakdown) throws Exception {
        if (null == oIndexBreakdown) {
            return AjaxResult.error("参数为空！");
        }
        List<OIndexBreakdown> oIndexBreakdownList = oIndexBreakdownService.cityIndexBrekdown(oIndexBreakdown);
        return AjaxResult.success(oIndexBreakdownList);
    }


    /*
    *  区县指标分解
    * */
    @RequestMapping("/districtBreakDown")
    public AjaxResult districtBreakDown(@RequestBody Map map) throws Exception {
        if (null == map){
            return AjaxResult.error("参数为空！");
        }
        List<OIndexBreakdown> oIndexBreakdownList = (List<OIndexBreakdown>)map.get("OIndexBreakdownList");
        List<OIndexBreakdown> oIndexBreakdowns = oIndexBreakdownService.districtBreakDown(oIndexBreakdownList);
        return AjaxResult.success(oIndexBreakdowns);
    }


    /*
    *  地市分解回显
    * */
    @RequestMapping("/getCityBreakDownData")
    public AjaxResult getCityBreakDownData(String year, String period){
        if (null == year || null == period){
            return AjaxResult.error("参数为空！");
        }
        List<OIndexBreakdown> oIndexBreakdowns = oIndexBreakdownService.getCityBreakDownData(year,period);
        return AjaxResult.success(oIndexBreakdowns);
    }

    /*
    *  判断是否可以做分解操作
    * */
    @RequestMapping("/isDoBreakDown")
    public AjaxResult isDoBreakDown(String year){
        if (null == year){
            return AjaxResult.error("参数为空！");
        }
        String msg = oIndexBreakdownService.isDoBreakDown(year);
        return AjaxResult.success(msg);
    }


    /*
    *  指标目标值查询
    * */
    @RequestMapping("/getIndexBrekdownByUnit")
    public AjaxResult getIndexBrekdownByUnit(OIndexBreakdown oIndexBreakdown) throws Exception {
        if (null == oIndexBreakdown || null == oIndexBreakdown.getUnit()) {
            return AjaxResult.error("参数为空！");
        }
        List<OIndexBreakdown> oIndexBreakdownList = oIndexBreakdownService.getIndexBrekdownByUnit(oIndexBreakdown);
        return AjaxResult.success(oIndexBreakdownList);
    }

    /**
     *  新增数指标趋势
     * */
    @RequestMapping("/implementationIndicators")
    public AjaxResult implementationIndicators(String orgNo, String year) throws Exception {
        if (null == orgNo || null == year){
            return AjaxResult.error("参数为空！");
        }
        List<IndicatorExecutionDto> indicatorExecutionDtoList = oIndexBreakdownService.implementationIndicators(orgNo, year);
        return AjaxResult.success(indicatorExecutionDtoList);
    }

    /**
     *  新增数占比（各个区县）
     */
    @RequestMapping("/addNumPercentage")
    public AjaxResult addNumPercentage(String orgNo, String year) throws Exception {
        if (null == orgNo || null == year){
            return AjaxResult.error("参数为空！");
        }
        List<IndicatorExecutionDto> indicatorExecutionDtoList = oIndexBreakdownService.addNumPercentage(orgNo, year);
        return AjaxResult.success(indicatorExecutionDtoList);
    }


    /*
    *   指标明细数据列表(备注：添加时间段查询，只允许在一个月内操作，不能夸月选择)
    * */
    @RequestMapping("/getIndexDetailList")
    public TableDataInfo getIndexDetailList(String orgNo, String type, String startTime, String endTime) throws Exception {
        if (null == orgNo || null == type){
            List<String> list = new ArrayList<String>();
            list.add("参数为空！");
            return getDataTable(list);
        }
        startPage();
        List<OIndexBreakdown> oIndexBreakdownList = oIndexBreakdownService.getIndexDetailList(orgNo, type, startTime, endTime);
        return getDataTable(oIndexBreakdownList);
    }

}
